'''
Created on 23-10-2012

@author: Mateusz
'''
import math
import matplotlib.pyplot as plt
import random
from sets import Set

class Point(object):
    
    def __init__(self, x, y, _class='N'):
        self.x = x
        self.y = y
        self._class = _class
        
    def dist(self, another_point):
        return math.sqrt(math.pow(self.y - another_point.y, 2) + math.pow(self.x - another_point.x, 2))

def drange(start, stop, step):
    r = start
    while r < stop:
        yield r
        r += step
        
def isAnyNear(point, points, dist):
    for p in points:
        if(point.dist(p) < dist):
            return 'Y'
    return 'N'

if __name__ == '__main__':
    N = 3200
    density = N / 100
    r_max = 1
    x_points_tren = []
    y_points_tren = []
    file = open("spirala.data", "w+")
    treshold = 0.05
    points_ideal = []
    for i in range(1, N):
        teta_i = i * math.pi / float((16 * density))
        r_i = (r_max * (104 * density) - i) / float(104 * density)
       
        x_i = r_i * math.cos(teta_i) 
        y_i = r_i * math.sin(teta_i)
        point = Point(x_i, y_i, 'Y') 
        points_ideal.append(point)
         
#        x_i = x_i + random.uniform(-treshold, treshold)
#        y_i = y_i + random.uniform(-treshold, treshold)
#        x_points_tren.append(x_i)
#        y_points_tren.append(y_i)
#        file.write(str(x_i) + "," + str(y_i) + "," + "Spirala\n")

    test_training_set = Set()
    while(len(test_training_set) < 2000):
    #generate random point in (-1,-1) - (1,1) space
        random_point = Point(random.uniform(-1,1), random.uniform(-1,1))
    #find if there exist point on spiral near generated point
        random_point._class = isAnyNear(random_point, points_ideal, 0.07)
        test_training_set.add(random_point)    
    file.close()
    fig = plt.figure()
    sub_plot = fig.add_subplot(111)
    p = sub_plot.plot([p.x for p in points_ideal],[p.y for p in points_ideal], 'y')
    p = sub_plot.plot([p.x for p in test_training_set if p._class=='Y'],[p.y for p in test_training_set if p._class=='Y'], 'b.')
    p = sub_plot.plot([p.x for p in test_training_set if p._class=='N'],[p.y for p in test_training_set if p._class=='N'], 'r.')
    fig.show()    
    file = open("spirala_test.data", "w+")
    for i in drange(-1, 1, 0.01):
        for j in drange(-1, 1, 0.01):
            file.write(str(i) + "," + str(j) + "," + "Test\n")
    file.close()
    raw_input() 
    
        
